package base;

import static util.Constant.STR_INVALID_ID;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/**
 * The Comment {@code class}. Create Comment object with an ID, an owner and a
 * content.
 * 
 * @author Peter NGO, Guillaume POUSSEL and Mathieu RUL
 * 
 * @version 1.0
 */
public class Comment {

	/**
	 * ID of the Comment.
	 */
	private Integer ID;

	/**
	 * Owner ID of the Comment.
	 */
	private Integer ownerID;

	/**
	 * Content of the Comment.
	 */
	private String content;

	/**
	 * Date of the Comment.
	 */
	private GregorianCalendar date;

	/**
	 * Constructor of the {@code class}. Create a Comment from an ID.
	 * 
	 * @param ID
	 *            the comment ID.
	 * 
	 * @throws IllegalArgumentException
	 */
	public Comment(Integer ID) throws IllegalArgumentException {
		if (ID < 0)
			throw new IllegalArgumentException(STR_INVALID_ID);

		this.ID = ID;
	}

	/**
	 * Constructor of the {@code class}. Create a Tag from an owner ID and a
	 * content.
	 * 
	 * @param ID
	 *            the ID of the comment.
	 * 
	 * @param ownerID
	 *            the owner ID.
	 * 
	 * @param content
	 *            the content of the comment.
	 * 
	 * @param date
	 *            the date of the comment.
	 * 
	 * @throws IllegalArgumentException
	 */
	public Comment(Integer ID, Integer ownerID, String content,
			GregorianCalendar date) throws IllegalArgumentException {
		if (ID < 0)
			throw new IllegalArgumentException(STR_INVALID_ID);

		this.ID = ID;
		this.ownerID = ownerID;
		this.content = content;
		this.date = date;
	}

	/**
	 * Constructor of the {@code class}. Create a Tag from an owner ID and a
	 * content.
	 * 
	 * @param ownerID
	 *            the owner ID.
	 * 
	 * @param content
	 *            the content of the comment.
	 */
	public Comment(Integer ownerID, String content) {
		this(0, ownerID, content, new GregorianCalendar());
	}

	/**
	 * Check equality between two comments.
	 * 
	 * If this method get a Comment, it compares the two IDs, else it returns
	 * false.
	 * 
	 * @param object
	 *            Another object to compare.
	 * 
	 * @return {@code true} if the parameter are a Comment; {@code false}
	 *         otherwise.
	 */
	public boolean equals(Object object) {
		return ((object instanceof Comment) && ((Comment) object).getID() == this.ID);
	}

	/**
	 * Return the Comment's content.
	 * 
	 * @return the content.
	 */
	public String getContent() {
		return this.content;
	}

	/**
	 * Return date of the Comment.
	 * 
	 * @return date of the comment.
	 */
	public GregorianCalendar getDate() {
		return this.date;
	}

	/**
	 * Return the comment's ID.
	 * 
	 * @return the ID.
	 */
	public Integer getID() {
		return ID;
	}

	/**
	 * Return the comment's owner ID.
	 * 
	 * @return the owner ID.
	 */
	public Integer getOwnerID() {
		return this.ownerID;
	}

	/**
	 * Get an hash representation of the Comment.
	 * 
	 * @return the hash representation of the Comment.
	 */
	@Override
	public int hashCode() {
		return null != ID ? ID : 0;
	}

	/**
	 * Set the Comment's content.
	 * 
	 * @param content
	 *            The content.
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * Set the Comment's date.
	 * 
	 * @param date
	 *            The date.
	 */
	public void setDate(GregorianCalendar date) {
		this.date = date;

	}

	/**
	 * Set the owner ID.
	 * 
	 * @param ID
	 *            The owner ID.
	 * 
	 * @throws IllegalArgumentException
	 */
	public void setOwnerID(Integer ID) throws IllegalArgumentException {
		if (ID < 0)
			throw new IllegalArgumentException(STR_INVALID_ID);

		this.ownerID = ID;
	}

	/**
	 * Returns a {@code String} object representing the Comment's value.
	 * 
	 * @return a string representation of the Comment.
	 */
	@Override
	public String toString() {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		return "Comment " + ID + " (" + dateFormat.format(date.getTime())
				+ ") : \"" + content + "\"";
	}

}
